/**
 * Copyright (c) 2008, Keisuke Inoue, Syracuse Unviersity
 * All rights reserved.
 *
 */
package edu.syr.iis.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import edu.syr.iis.web.model.ConversationLabel;
import edu.syr.iis.web.model.ConversationLabelManager;

/**
 * @author kinoue
 *
 */
public class EnterConversationLabelController extends SimpleFormController
{
    private ConversationLabelManager conversationLabelManager = null;
    
    public static final String CLABEL_ID = "clabelId";
    public static final String CLABELS_KEY = "clabels";
        
    /**
     * Returns a new instance of ConversationLabel object if "conversationLabelId" 
     * HTTP parameter is specified; other returns instance of object in database 
     * matching the value of the parameter.
     * @see ConversationLabel
     */
    	
    @Override
	protected Object formBackingObject(HttpServletRequest request)
    {
	System.out.println("Entering EnterConversationLabelController!");
	
	ConversationLabel conversationLabel = null;
        
	if (request.getParameter(CLABEL_ID) != null
                && request.getParameter(CLABEL_ID).trim().length() > 0) 
	{
            int conversationId = Integer.parseInt(request.getParameter(CLABEL_ID));
            conversationLabel = conversationLabelManager.getConversationLabel(conversationId);
        } else 
        {
            conversationLabel = new ConversationLabel();
        }
	/*
	conversationLabel.setConversationLabelId(123);
	conversationLabel.setLabel("Enter the label.");
	conversationLabel.setDescription("Enter your description here.");
	*/
	
	System.out.println("Return converlationLabel with ID: " + conversationLabel.getConversationLabelId());
        return conversationLabel;
    }   

    @Override
	protected Map referenceData(HttpServletRequest request) throws Exception
    {
        HashMap model = new HashMap();
        model.put(CLABELS_KEY, conversationLabelManager.getConversationLabels());
        return model;
    }

    
    /**
     * Saves ConversationLabel command object using 
     * conversationLabelManager.saveConversationLabel(conversationLabel);
     * @see ConversationLabelManager
     */
        
    @Override
	protected ModelAndView onSubmit(
            HttpServletRequest request,
            HttpServletResponse response,
            Object command,
            BindException errors)
    {
	ConversationLabel conversationLabel = (ConversationLabel) command;
	
	System.out.println("Entinering onSubmit with ");
	System.out.println(" id:    " + conversationLabel.getConversationLabelId());
	System.out.println(" label: " + conversationLabel.getLabel());
	System.out.println(" desc: " + conversationLabel.getDescription());
        
        conversationLabelManager.saveConversationLabel(conversationLabel);
/*        
        request.getSession().setAttribute(
                "message",
                getMessageSourceAccessor().getMessage(
                        "message.enterhours.savesuccess"));
                        */
        return new ModelAndView(getSuccessView());
    }

    public ConversationLabelManager getConversationLabelManager()
    {
        return conversationLabelManager;
    }

    public void setConversationLabelManager(
    	ConversationLabelManager conversationLabelManager)
    {
        this.conversationLabelManager = conversationLabelManager;
    }

    	
}
